home *** CD-ROM | disk | FTP | other *** search
Text File | 1996-03-13 | 4.7 KB | 197 lines | [TEXT/CWIE] |
- // File: Scanners.h
- //
- // Sources for each of our character counting routines
- //
-
- #include "scanners.h"
- #ifndef __LOWMEM__
- #include <LowMem.h>
- #endif
-
-
- static int kBufferSize = 128 * 1024;
-
- // --------------------------------------------------------------------------------
-
- int countChars(FSSpecPtr fsp)
- {
- // Count the number of times the letter
- // A appears in the file
- FILE *f = NULL;
- int counter = 0;
- char currChar;
- char filename[64];
-
- BlockMove((Ptr)&fsp->name[1], filename, fsp->name[0]);
- filename[fsp->name[0]] = '\0';
-
- f = fopen(filename, "r");
- while ((currChar = fgetc(f)) != EOF) {
- if (currChar == 'A') counter += 1;
- }
- fclose(f);
- return counter;
- }
-
- // --------------------------------------------------------------------------------
-
- int countCharsFS(FSSpecPtr fsp)
- {
- // Count the number of times the letter
- // A appears in the file reading the file in
- // blocks
- int counter = 0;
- char *buffer, *currChar;
- short refNum;
- long charCount;
- OSErr err;
-
- err = FSpOpenDF(fsp, fsRdPerm, &refNum);
- if (err == noErr) {
- buffer = (char*)NewPtr(kBufferSize);
- if (buffer != nil) {
- for (;;) {
- charCount = kBufferSize;
- err = FSRead(refNum, &charCount, (Ptr)buffer);
- if ((err != noErr) && (err != eofErr)) break;
- if (charCount == 0) break;
- currChar= buffer;
- while (charCount-- > 0) {
- if (*currChar++ == 'A') counter++;
- }
- }
- DisposePtr(buffer);
- }
- FSClose(refNum);
- }
- return counter;
- }
-
- // --------------------------------------------------------------------------------
-
- int countCharsFS2(FSSpecPtr fsp)
- {
- // Count the number of times the letter
- // A appears in the file after reading the
- // whole file into memory
- int counter = 0;
- char *buffer, *currChar;
- Handle hBuffer = nil;
- short refNum;
- long charCount;
- OSErr err;
-
- err = FSpOpenDF(fsp, fsRdPerm, &refNum);
- if (err == noErr) {
- // get the file size, set up an area to hold it all,
- // and read the file
- (void) GetEOF(refNum, &charCount);
-
- // Decide where to allocate the memory
- // We'll use temporary memory if there's not enough
- // room in the main memory
- if (FreeMem() >= charCount)
- hBuffer = NewHandle(charCount);
- else if ((hBuffer == nil) && (TempFreeMem() >= charCount))
- hBuffer = TempNewHandle(charCount, &err);
-
- if (hBuffer != nil) {
- HLock(hBuffer);
- buffer = (char*)*hBuffer;
- (void) FSRead(refNum, &charCount, (Ptr)buffer);
- }
- FSClose(refNum);
-
- if (buffer != NULL) {
- currChar= buffer;
- while (charCount-- > 0) {
- if (*currChar++ == 'A') counter++;
- }
- DisposeHandle(hBuffer);
- }
- }
- return counter;
- }
-
- // --------------------------------------------------------------------------------
-
- void setup (ParmBlkPtr pb, short refNum, short vRefNum, long bufSize);
- void destroy (ParmBlkPtr pb);
-
- void setup (ParmBlkPtr pb, short refNum, short vRefNum, long bufSize)
- {
- pb->ioParam.ioCompletion = NULL;
- pb->ioParam.ioResult = 1;
- pb->ioParam.ioRefNum = refNum;
- pb->ioParam.ioVRefNum = vRefNum;
- pb->ioParam.ioReqCount = bufSize;
- pb->ioParam.ioBuffer = NewPtr(bufSize);
- pb->ioParam.ioPosMode = fsFromMark;
- pb->ioParam.ioPosOffset = 0;
- }
-
- void destroy (ParmBlkPtr pb)
- {
- DisposePtr(pb->ioParam.ioBuffer);
- DisposePtr((Ptr)pb);
- }
-
- int countCharsAsync(FSSpecPtr fsp)
- {
- // Count the number of times the letter
- // A appears in the file, reading the file
- // one character at a time
- int counter = 0;
- ParmBlkPtr pb[2], currPBPtr;
- int currPB = 0;
- char *buffer, *currChar;
- short refNum;
- long charCount;
- OSErr err;
-
- // Allocate parameter blocks
- // Open the file
- err = FSpOpenDF(fsp, fsRdPerm, &refNum);
- if (err == noErr) {
- // Set up parameter blocks
- pb[0] = (ParmBlkPtr)NewPtrClear(sizeof(ParamBlockRec));
- pb[1] = (ParmBlkPtr)NewPtrClear(sizeof(ParamBlockRec));
- setup(pb[0], refNum, fsp->vRefNum, kBufferSize);
- setup(pb[1], refNum, fsp->vRefNum, kBufferSize);
-
- // Start 2 read operations going
- (void) PBReadAsync(pb[0]);
- (void) PBReadAsync(pb[1]);
- currPBPtr = pb[0];
-
- for (;;) {
- // Wait for the I/O operation to complete
- while (currPBPtr->ioParam.ioResult > 0) {};
-
- // The data is ready, so count the characters
- buffer = currPBPtr->ioParam.ioBuffer;
- charCount = currPBPtr->ioParam.ioActCount;
- if (charCount == 0) break;
- currChar= buffer;
- while (charCount-- > 0) {
- if (*currChar++ == 'A') counter++;
- }
-
- // Put this buffer back into the reading queue
- (void) PBReadAsync(currPBPtr);
-
- // Switch to the other buffer
- currPB = 1 - currPB;
- currPBPtr = pb[currPB];
- currPBPtr->ioParam.ioPosMode = fsAtMark;
- }
- KillIO(refNum);
- destroy(pb[0]);
- destroy(pb[1]);
- FSClose(refNum);
- }
- return counter;
- }
-
-
-